﻿using System;
using System.Data;
using System.Deployment.Application;
using System.Security.Principal;
using System.Windows.Forms;
using GianosNET.ClientApp.Properties;

namespace ClientApp
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();

            lblVersion.Text = string.Format("v. {0}", Application.ProductVersion);
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            WindowsIdentity wi = WindowsIdentity.GetCurrent();
            tbUsername.Text = wi.Name;

            Classes.sharedObj.Servers = new DataTable();
            Classes.sharedObj.Servers.Columns.Add("name", typeof(string));
            Classes.sharedObj.Servers.Columns.Add("value", typeof(string));
            
            string[] tmp = Settings.Default.availableServer.Split('|');
            foreach (string s in tmp)
            {
                if (ApplicationDeployment.IsNetworkDeployed && s.Contains("localhost"))
                    continue;

                if (s.Contains(";"))
                {
                    string[] tmp2 = s.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);

                    Classes.sharedObj.Servers.Rows.Add(tmp2.Length > 1
                                                           ? new object[] {tmp2[0], tmp2[1]}
                                                           : new object[] {tmp2[0], tmp2[0]});
                }
                else 
                    Classes.sharedObj.Servers.Rows.Add(new object[] { s, s });
            }

            cbServer.DataSource = Classes.sharedObj.Servers;
            cbServer.DisplayMember = "name";
            cbServer.ValueMember = "value";

            if (cbServer.Items.Count > 0)
                cbServer.SelectedIndex = 0;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (cbServer.SelectedValue != null)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
                return;
            }

            MessageBox.Show("Scegliere un server");
        }

        public string Server
        {
            get { return cbServer.SelectedValue.ToString(); }
        }
        public string Username
        {
            get { return tbUsername.Text; }
        }
        public string Password
        {
            get { return tbPassword.Text; }
        }


        private void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                btnLogin_Click(this, new EventArgs());
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
    }
}